package com.example.yblin.hello24;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yblin.hello24.DataUtils.LoginUtil;
import com.example.yblin.hello24.DataUtils.NetUtil;

public class LoginActivity extends AppCompatActivity {

    private TextView btRegister;
    private ImageView btBack;
    private TextView btLogin;
    private EditText etUsername;
    private EditText etPassword;

    private String username;
    private String password;
    private Toast toast;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        btRegister = findViewById(R.id.textView_sign_up);
        btBack = findViewById(R.id.back_login);
        btLogin = findViewById(R.id.button_login);
        toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);

        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (validate()) {//用户名密码输入了
                    Log.i("swsqwdqed", username + " " + password);
                    new SignInAsync().execute();
                }
            }
        });
        etUsername = findViewById(R.id.editText_username);
        etPassword = findViewById(R.id.editText_password);

        etUsername.requestFocus();
    }

    /**
     * 用于验证用户名密码是否输入
     */
    private boolean validate() {
        username = etUsername.getText().toString();
        password = etPassword.getText().toString();

        if (username == null || username.equals("")) {
            toast.cancel();
            toast = Toast.makeText(this, "请输入用户名！", Toast.LENGTH_SHORT);
            toast.show();
            return false;
        } else if (password == null || password.equals("")) {
            toast.cancel();
            toast = Toast.makeText(this, "请输入密码！", Toast.LENGTH_SHORT);
            toast.show();
            return false;
        }
        return true;
    }


    public class SignInAsync extends AsyncTask<Void, Void, Integer> {

        @Override
        protected void onPreExecute() {
            btLogin.setText("正在登录...");
            btLogin.setEnabled(false);
        }

        @Override
        protected Integer doInBackground(Void... params) {
            return LoginUtil.login(username, password,
                    LoginActivity.this);
        }

        @Override
        protected void onPostExecute(Integer result) {
            btLogin.setText("登录");
            btLogin.setEnabled(true);
            if (result == NetUtil.CODE_OK) {
                LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
            } else if (result == NetUtil.CODE_USERNAME_OR_PASSWORD_ERROR) {
                toast = Toast.makeText(LoginActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT);
                toast.show();
            } else {
                toast = Toast.makeText(LoginActivity.this, "网络异常！", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }
}